<template>
  <el-dialog title="编辑种类" :close-on-click-modal="false" :visible.sync="iseditUserDialog">
    <el-form :model="gridCategory" ref="editForm" label-width="80px">
        <el-form-item label="名称" prop="name">
            <el-input v-model="gridCategory.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父分类">
            <el-input v-model="lastName" :disabled="true" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
            <el-input-number v-model="gridCategory.sort" controls-position="right" :min="0" ></el-input-number>
        </el-form-item>
        <el-form-item label="是否显示" prop="online">
            <el-switch
                v-model="gridCategory.online"
                active-color="#13ce66"
                inactive-color="#ff4949"
                >
            </el-switch>
        </el-form-item>
        <el-form-item label="图片" prop="img">
            <el-upload
                class="avatar-uploader"
                :action="objData.host"
                :data="objData"
                :show-file-list="false"
                :before-upload="ossPolicy">
                <img v-if="gridCategory.img" :src="gridCategory.img" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="描述" prop="description">
            <el-input v-model="gridCategory.description" autocomplete="off"></el-input>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="iseditUserDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
    data(){
        return{
            current : '',
            gridCategory:{},
            iseditUserDialog : false,
            objData:{
                OSSAccessKeyId:'',
                policy:'',
                Signature:'',
                key:'',
                host:'',
                dir:''
            },
            lastName:''
        }
    },
    methods:{
        submitEdit(){
            let editData ={
                id : this.gridCategory.id,
                name : this.gridCategory.name,
                sort : this.gridCategory.sort,
                online : this.gridCategory.online == true? 1:0,
                img : this.gridCategory.img,
                description : this.gridCategory.description
            }
            this.$http.put('/cate/son',editData).then(res=>{
                if(res.data.code ==0){
                    this.$message({
                    type: 'success',
                    message: '修改成功!',
                    });
                    this.iseditUserDialog = false
                    this.$emit('refresh',this.current)
                }else{
                    this.$message.error('修改失败');
                }
            })
        },
        ossPolicy(file){
            //在上传前进行服务器签名
            return new Promise((resolve,reject)=>{
                //请求后端
                this.$http.get('/policy').then(res=>{
                    this.objData.OSSAccessKeyId = res.data.data.accessid
                    this.objData.policy = res.data.data.policy
                    this.objData.Signature = res.data.data.signature
                    this.objData.dir = res.data.data.dir
                    this.objData.host = res.data.data.host
                    this.objData.key = res.data.data.dir+'${filename}';
                    this.gridCategory.img = `${this.objData.host}/${this.objData.dir}${file.name}`
                    resolve(true) //继续上传
                })
                .catch(function(error){
                    console.log(error);
                    reject(false) //停止上传
                })
            })
        },
    }
}
</script>

<style>

</style>